<?php

namespace app\common\model\distribute;

use app\common\model\hfztcy\LibBalanceRecords;
use common\Common;

class StoreUserBySeeVideoCommission extends base
{
    public function getCommissionByStoreUserId ()
    {
        $result = [];
        $this->write_file('-----------门店-----------');
        if (isset($this->orderInfo['store_user_id']) && $this->orderInfo['store_user_id'])
        {
            $storeUserId = $this->orderInfo['store_user_id'];
            $storeUserMoney = $this->orderInfo['md_commission_money'];
            $this->write_file('门店用户：' . $storeUserId);
            $this->write_file('门店用户佣金：' . $storeUserMoney);
            $findStoreMemberRes = $this->member->findMember(['user_id' => $storeUserId]);
            if ($findStoreMemberRes['errorCode'] == 0)
            {
                $storeMemberInfo = $findStoreMemberRes['data'];
                $data = $this->getData($storeMemberInfo, $storeUserMoney);
                $this->member->updateMember(['user_id' => $storeUserId], $data);
                $sql1 = $this->member->getLastSql();
                $this->write_file('更新会员佣金SQL：' . $sql1);
                $this->user->updateUser(['id' => $storeUserId], $data);
                $sql2 = $this->user->getLastSql();
                $this->write_file('更新会员佣金SQL：' . $sql2);
                // 添加会员佣金记录
                $sql3 = $this->extracted($storeUserMoney, $storeMemberInfo['balance'], $storeUserId);
                $this->write_file('添加佣金记录SQL：' . $sql3);
                $result[] = [
                    'update_member_balance1_sql' => $sql1,
                    'update_member_balance2_sql' => $sql2,
                    'create_member_commission_record_sql' => $sql3
                ];
            } else {
                $this->write_file('用户不存在' );
            }
        }

        return $result;
    }

    private function getData($memberInfo, $commission)
    {
        $total_balance = (float)$memberInfo['total_balance'] + (float)$commission;//累计佣金
        $balance = (float)$memberInfo['balance'] + (float)$commission;//可提现佣金
        return [
            'total_balance' => $total_balance,
            'balance' => $balance,
        ];
    }

    private function extracted($money, $balance, $userId)
    {
        $sql = '';
        if (floatval($money) > 0) {
            $type = $this->orderInfo['type'];
            $typeStr = '';
            if ($type == 0) $typeStr = '付费看视频';
            if ($type == 1) $typeStr = '购买VIP套餐';
            if ($type == 2) $typeStr = '栏目认证';
            if ($type == 3) $typeStr = '红娘认证';
            $curPayUser = $this->curPayUserInfo;
            if ($curPayUser['errorCode'] == 0) {
                $info = $curPayUser['data']['username'] . $typeStr . '，可得佣金：' . $money;
            } else {
                $info = $typeStr . '，可得佣金：' . $money;
            }
            $data = [];
            $data['user_id'] = $userId;
            $data['describe'] = $info;
            $data['before_balance'] = $balance;
            $data['amount'] = $money;
            $data['add_time'] = Common::getTime();
            $data['order_id'] = $this->orderInfo['id'];
            $data['pay_user_id'] = $this->orderInfo['user_id'];
            $data['type'] = $this->type;
            $data['zj_state'] = 0;//佣金状态 0可提现 1冻结中 2已失效
            $data['order_info'] = json_encode($this->orderInfo, JSON_UNESCAPED_UNICODE);
            $this->balanceRecords->addCommissionRecords($data);
            $sql = $this->balanceRecords->getLastSql();
        }
        return $sql;
    }
}